//
//  Task_GetWsbsGroup.m
//  EpointFrame5
//
//  Created by AndLi on 15/4/22.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//  网上办事--获取分组

#import "Task_GetWsbsGroup.h"

@implementation Task_GetWsbsGroup
-(void)execute
{
    
    NSString *url           = NSLocalizedString(@"Epoint_DNet_WMH", @"");
    NSString *ValidateData  = [EpointBizTools getToken];
    NSString *namespace     = NSLocalizedString(@"Epoint_DNet_WMH_NameSpace", @"");
    
    //请求接口
    NSString *method = @"GetWsbsGroup";
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras></paras>]]>";
    
    NSString *key = [NSString stringWithFormat:@"%@",method];
    
    //请求接口
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:namespace method:method];
    [ws addParameterForString:@"ValidateData" value:ValidateData];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs =  ws.start;
    
    NSMutableArray *result = [self validateXMLWS2:bs];
    NSString *msg = [result objectAtIndex:1];
    if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
        [DBFrameService setCacheValue:key value:bs];
        [self exeSuccessWithData:[Task_GetWsbsGroup getLists:bs]];
    }
    else
    {
        [self userCache:key message:msg];
    }
    
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:[Task_GetWsbsGroup getLists:cacheBS]];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

#pragma mark 解析XML
+(NSMutableArray *) getLists:(NSString *)result
{
    NSString *UserArea = [EpointStringUtil getXMLFormatedAttributeBsWithTag:result xmlTag:@"CateList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:UserArea  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"Cate"];
    NSMutableArray *dataList = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays) {
        
        WSBSCateListModel *f = [[WSBSCateListModel alloc]init];
        
        GDataXMLElement *CateName = [[arr elementsForName:@"CateName"] objectAtIndex:0];
        f.CateName = [CateName stringValue];
        
        GDataXMLElement *CateNum = [[arr elementsForName:@"CateNum"] objectAtIndex:0];
        f.CateNum = [CateNum stringValue];
        
        GDataXMLElement *CateGuid = [[arr elementsForName:@"CateGuid"] objectAtIndex:0];
        f.CateGuid = [CateGuid stringValue];
        
        GDataXMLElement *IsLeaf = [[arr elementsForName:@"IsLeaf"] objectAtIndex:0];
        f.IsLeaf = [IsLeaf stringValue];
        
        [dataList addObject:f];
    }
    return dataList;
}

//<?xml version="1.0" encoding="gb2312" ?>
//<EpointDataBody>
//<DATA>
//<ReturnInfo>
//<Status>True</Status>
//<Description></Description>
//</ReturnInfo>
//<UserArea>
//<CateList>
//<Cate>
//<CateName><![CDATA[个人办事]]></CateName>
//<CateGuid>0c7602ec-6ecd-4ef2-b7d5-f5822a70456d</CateGuid>
//</Cate>
//<Cate>
//<CateName><![CDATA[企业办事]]></CateName>
//<CateGuid>afc5f953-d6bd-4027-8592-81e1dc055402</CateGuid>
//</Cate>
//</CateList>
//</UserArea>
//</DATA>
//</EpointDataBody>


@end
